package hczc.longcai.com.hccrowdfunding.home.third_rankinglist;


import android.graphics.Color;
import android.media.tv.TvContentRating;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.PropertyPermission;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.home.third_rankinglist.fragments.RankingListCrowdfundingFragment;
import hczc.longcai.com.hccrowdfunding.home.third_rankinglist.fragments.RankingListIncomeFragment;
import hczc.longcai.com.hccrowdfunding.home.third_rankinglist.fragments.RankingListPopularityFragment;

/**
 * 排行榜
 * A simple {@link Fragment} subclass.
 */
public class ThirdFragment extends Fragment implements ViewPager.OnPageChangeListener, View.OnClickListener {

    private ViewPager viewPager;
    private TextView popularity;
    private TextView crowdfunding;
    private TextView income;
    private RankingListPopularityFragment pupularityFra;
    private RankingListCrowdfundingFragment crowdfundingFragment;
    private RankingListIncomeFragment incomeFragment;

    private List<Fragment> list = new ArrayList<>();
    private List<TextView> textViews = new ArrayList<>();

    private MyPagerAdapter adapter;


    public ThirdFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_third, container, false);
        BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));

        initFragments();
        initViews(view);


        return view;
    }

    private void initFragments() {
        pupularityFra = new RankingListPopularityFragment();
        list.add(pupularityFra);
        crowdfundingFragment = new RankingListCrowdfundingFragment();
        list.add(crowdfundingFragment);
        incomeFragment = new RankingListIncomeFragment();
        list.add(incomeFragment);
    }

    private void initViews(View view) {
        viewPager = ((ViewPager) view.findViewById(R.id.rankinglist_viewpager));
        viewPager.setOnPageChangeListener(this);
        adapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
        viewPager.setAdapter(adapter);

        popularity = ((TextView) view.findViewById(R.id.rankinglist_tabs_project_popularity));
        popularity.setOnClickListener(this);
        popularity.setTextColor(Color.parseColor("#2b2a2a"));
        textViews.add(popularity);
        crowdfunding = ((TextView) view.findViewById(R.id.rankinglist_tabs_expert_crowdfunding));
        crowdfunding.setOnClickListener(this);
        textViews.add(crowdfunding);
        income = ((TextView) view.findViewById(R.id.rankinglist_tabs_expert_income));
        income.setOnClickListener(this);
        textViews.add(income);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        int len = textViews.size();
        for (int i = 0; i < len; i++){
            textViews.get(i).setTextColor(Color.parseColor("#969696"));
        }
        textViews.get(position).setTextColor(Color.parseColor("#2b2a2a"));
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View view) {
        int index = view.getId();
        switch (index){
            case R.id.rankinglist_tabs_project_popularity:
                viewPager.setCurrentItem(0);
                break;
            case R.id.rankinglist_tabs_expert_crowdfunding:
                viewPager.setCurrentItem(1);
                break;
            case R.id.rankinglist_tabs_expert_income:
                viewPager.setCurrentItem(2);
                break;
        }
    }

    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }


}
